package com.yc.sole.erp.controller;

import com.yc.sole.erp.biz.ProcessBiz;
import com.yc.sole.erp.request.ProcessEmployeeReq;
import com.yc.sole.erp.request.ProcessQueryReq;
import com.yc.sole.erp.request.ProcessReq;
import com.yc.sole.erp.response.EmployeeResp;
import com.yc.sole.erp.response.ProcessResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yizuomin
 * @date 2024-03-06
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class ProcessController {

    private final ProcessBiz processBiz;

    /**
     * 新增
     *
     * @param processReq
     * @return RestResult
     */
    @PreAuth(value = "erp:process:save", description = "新增")
    @PostMapping("/api/erp/process/saveProcess")
    public Result<Void> saveProcess(@RequestBody @Validated(SaveGroup.class) ProcessReq processReq) {
        processBiz.saveProcess(processReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param processReq
     * @return RestResult
     */
    @PreAuth(value = "erp:process:update", description = "修改")
    @PostMapping("/api/erp/process/updateProcessById")
    public Result<Void> updateProcessById(@RequestBody @Validated(UpdateGroup.class) ProcessReq processReq) {
        processBiz.updateProcessById(processReq);
        return Result.success();
    }

    /**
     * 通过id批量删除
     *
     * @param baseBatchIdsReq
     * @return RestResult
     */
    @PreAuth(value = "erp:process:delete", description = "删除")
    @PostMapping("/api/erp/process/deleteProcess")
    public Result<Void> deleteProcess(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseBatchIdsReq) {
        processBiz.deleteProcessByIds(baseBatchIdsReq.getIds());
        return Result.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return Result
     */
    @PreAuth(value = "erp:process:select", description = "查询")
    @PostMapping("/api/erp/process/selectProcessById/{id}")
    public Result<ProcessResp> selectProcessById(@PathVariable Long id) {
        return Result.data(processBiz.selectProcessById(id));
    }

    /**
     * 查询所有可用工序
     *
     * @return Result
     */
    @PreAuth(value = "erp:process:select", description = "查询")
    @PostMapping("/api/erp/process/selectAllProcess")
    public Result<List<ProcessResp>> selectAllProcess() {
        return Result.data(processBiz.selectAllProcess());
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:process:select", description = "查询")
    @PostMapping("/api/erp/process/selectProcessPage")
    public Result<Paging<ProcessResp>> selectProcessPage(@RequestBody @Validated BasePageReq<ProcessQueryReq> basePageReq) {
        return Result.data(processBiz.selectProcessPage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 查询工序关联员工
     *
     * @return Result
     */
    @PreAuth(value = "erp:process:select", description = "查询工序关联员工")
    @PostMapping("/api/erp/process/selectEmployeeByProcessId/{id}")
    public Result<List<EmployeeResp>> selectEmployeeByProcessId(@PathVariable Long id) {
        return Result.data(processBiz.selectEmployeeByProcessId(id));
    }


    /**
     * 修改工序关联员工
     *
     * @param processEmployeeReq
     * @return Result
     */
    @PreAuth(value = "erp:process:update", description = "修改工序关联员工")
    @PostMapping("/api/erp/process/updateProcessEmployee")
    public Result<Void> updateProcessEmployee(@RequestBody @Validated(UpdateGroup.class) ProcessEmployeeReq processEmployeeReq) {
        processBiz.updateProcessEmployee(processEmployeeReq);
        return Result.success();
    }
}
